package com.ctsi.bigdata.todoitem.controller;

import com.ctsi.bigdata.todoitem.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.bigdata.todoitem.dao.pojo.Item;
import java.util.List;

/**
 * Created by admin on 2015/11/18.
 */

@RestController
//@EnableAutoConfiguration
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/items")
    @ResponseBody
    public List<Item> list() {

        List<Item> list=itemService.itemList();
        return list;
    }

    @RequestMapping("/itemList")
    @ResponseBody
    public List<Item> itemList(Integer pageNo,Integer pageSize) {
        List<Item> list=itemService.items(pageNo==null?0:pageNo,pageSize==null?10:pageSize);
        return list;
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public String addItems(Item item){
     int result=itemService.addItem(item);
        if(result>0){
            return "add item success";
        }else{
            return "add item fail ";
        }

    }

    @RequestMapping("/deleteItem")
    @ResponseBody
    public String deleteItems(Long itemId){
        if(itemId==null)
            return "itemId not null";
        int result=itemService.deleteItem(itemId);
        if(result>0){
            return "delete item success";

        }else{
            return "delete item fail ";
        }

    }

    @RequestMapping("/searchItem")
    @ResponseBody
    public List<Item> searchItem(String  itemName){
        if(itemName==null)
            return null;
        return itemService.searchByItemName(itemName);

    }

}
